package com.bw.module_mine.ui.mine

import android.view.View
import androidx.fragment.app.viewModels
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_common.Router
import com.bw.lib_common.base.BaseFragment
import com.bw.lib_common.livedata_eventbus.Events
import com.bw.lib_common.livedata_eventbuss.LiveEventBus
import com.bw.lib_common.utils.ToastUtils
import com.bw.lib_storage.UserMMKV
import com.bw.module_mine.R
import com.bw.module_mine.databinding.FragmentMineBinding
import com.bw.module_mine.model.TabItem
import com.bw.module_mine.ui.mine.adapter.ToolAdapter
import dagger.hilt.android.AndroidEntryPoint

/**
 * 1.登录状态更新UI
 * 2.订阅事件总线：登录和退出登录
 * 3，获得用户信息并展示
 * 4.服务recyclerview展示
 */
@AndroidEntryPoint
@Route(path = Router.Ui.MineFragment)
class MineFragment : BaseFragment<MineViewModel,FragmentMineBinding>() {
    private val mViewModel:MineViewModel by viewModels()
    override val mLayoutResId: Int
        get() = R.layout.fragment_mine
    companion object {
        const val CLUB_MGR = "club_mgr"
        const val DVR = "dvr"
        const val GROUP = "group"
        const val MSG_CENTER = "msg_center"
        const val STOLEN = "stolen"
        const val APP_SHARE = "share"
        const val HELP_CENTER = "help_center"
        const val WIKI = "wiki"
        const val CUSTOMER = "customer"
        const val JOIN = "join"
        const val SETTINGS = "settings"

        const val STORE_SEARCH = "store_search"
        const val BOOKING_DRV = "booking_drv"
        const val RESCUE = "rescue"
        const val CARE_SERVICE = "care_service"
        const val FIX_ONLINE = "fix_online"

        const val MALL_ORDER = "store_search"
        const val MALL_FAVORITES = "mall_favorites"
        const val MALL_COUPON = "mall_coupon"
        const val CYCLONE_BEAN = "cyclone_bean"

        const val CHAT = "chat"
        const val ROUTE = "route"


        const val MY_CAR = "my_car"
        const val DRIVING_DATA = "driving_data"
        const val DRIVING_GROUP = "driving_group"
        const val MY_ACTIVITY = "my_activity"
    }
    private val mToolTabItem by lazy {
        arrayListOf(
            TabItem(CLUB_MGR, getString(R.string.string_club_management), R.mipmap.icon_club_management, false),
            TabItem(WIKI, getString(R.string.string_menu_wiki), R.mipmap.icon_menu_wiki, false),
            // TabItem(MSG_CENTER, getString(R.string.string_message_centre), R.mipmap.icon_mine_message_centra, true),
            TabItem(STOLEN, getString(R.string.string_stolen_applications), R.mipmap.icon_stolen_applications, false),
            TabItem(APP_SHARE, getString(R.string.string_app_share), R.mipmap.icon_app_share, false),
            TabItem(HELP_CENTER, getString(R.string.string_help_center), R.mipmap.icon_help, false),
            TabItem(CUSTOMER, getString(R.string.string_contact_customer), R.mipmap.icon_customer, false),
            TabItem(JOIN, getString(R.string.string_commercial_join), R.mipmap.icon_commercial_join, false),
            TabItem(SETTINGS, getString(R.string.string_settings_center), R.mipmap.icon_settings, false),
        )
    }

    private val mServiceTabItem by lazy {
        listOf(
            TabItem(STORE_SEARCH, "寻找门店", R.mipmap.icon_search_store, false),
            TabItem(BOOKING_DRV, "预约试驾", R.mipmap.icon_booking, false),
            TabItem(RESCUE, "一键救援", R.mipmap.icon_rescue, false),
            TabItem(CARE_SERVICE, "保养服务", R.mipmap.icon_care_service, false),
            TabItem(FIX_ONLINE, "在线报修", R.mipmap.icon_fix_online, false),
        )
    }

    private val mMallTabItem by lazy {
        listOf(
            TabItem(MALL_ORDER, "订单", R.mipmap.icon_mall_order, false),
            // TabItem(MALL_FAVORITES, "收藏", R.mipmap.icon_mall_favorites, false),
            TabItem(MALL_COUPON, "优惠券", R.mipmap.icon_mall_coupon, false),
            TabItem(CYCLONE_BEAN, "龙豆兑换", R.mipmap.icon_cyclone_bean, false),
        )
    }

    private val mMyCarTabItem by lazy {
        mutableListOf(
            TabItem(MY_CAR, "添加车辆", R.mipmap.icon_add_motor, false),
            //  TabItem(DRIVING_DATA, "历史骑行", R.mipmap.icon_driving_data, false)
            TabItem(MY_ACTIVITY, "我的活动", R.mipmap.icon_my_activity, false),
            TabItem(DRIVING_GROUP, "骑行组队", R.mipmap.icon_drive_group, false),
        )
    }
    private val mServiceAdapter by lazy {
        ToolAdapter(mServiceTabItem) {
            handleServiceTabClick(it.id)
        }
    }

    /**
     * 处理服务的点击
     */
    private fun handleServiceTabClick(id: String) {

    }

    private val mMallAdapter by lazy {
        ToolAdapter(mMallTabItem) {
            handleMallTabClick(it.id)
        }
    }
    /**
     * 处理商城订单的点击
     */
    private fun handleMallTabClick(id: String) {

    }

    private val myCarAdapter by lazy {
        ToolAdapter(mMyCarTabItem) {
            handleMyCarTabClick(it.id)
        }
    }
    /**
     * 处理我的车辆的点击
     */
    private fun handleMyCarTabClick(id: String) {

    }

    private val mToolAdapter by lazy {
        ToolAdapter(mToolTabItem) {
            handleToolTabClick(it.id)
        }
    }
    /**
     * 处理工具的点击
     */
    private fun handleToolTabClick(id: String) {

    }

    override fun initView() {
        super.initView()
        initShowView()
        mBinding.rvMall.adapter = mMallAdapter
        mBinding.rvMyCar.adapter = myCarAdapter
        mBinding.rvService.adapter = mServiceAdapter
        mBinding.rvTool.adapter = mToolAdapter
    }

    private fun initShowView() {
        if (UserMMKV.token.isNotEmpty()){
            onLogin() //已登录
        }else{
            onNotLogin() //未登录
        }
    }

    private fun onLogin() {
        mBinding.btnLogin.visibility = View.GONE
        mBinding.lUserInfo.visibility = View.VISIBLE

    }
    private fun onNotLogin() {
        mBinding.btnLogin.visibility = View.VISIBLE
        mBinding.lUserInfo.visibility = View.GONE
        mBinding.mineModel = null //数据为空
    }

    override fun initData() {
        super.initData()
        //获得用户信息并展示
        if (UserMMKV.token.isNotEmpty()){
            //获得用户信息
            mViewModel.getUserInfo()
            //获得用户车辆信息：网络请求
            //获得签到信息
            mViewModel.isSign()
        }else{
            //未登录
            onNotLogin()
        }
    }

    override fun initListener() {
        super.initListener()
        //订阅用户信息
        mViewModel.userInfoLiveData.observe(this){
            mBinding.mineModel = it
            mBinding.executePendingBindings()
        }
        //订阅签到结果
        mViewModel.signLiveData.observe(this){
            mBinding.tvRegister.text = if (it) "已签到" else "签到"
            mBinding.tvRegister.isEnabled = !it
        }
        //订阅签到失败结果
        mViewModel.signInLiveDataError.observe(this){
            ToastUtils.showLong(requireActivity(),"签到失败")
        }
        //订阅当前用户的车辆信息：用户是否有爱车，更新rvMyCar：无车辆：添加车辆 有车辆：我的爱车
        //签到
        mBinding.tvRegister.setOnClickListener{
            mViewModel.signIn()
        }

        //订阅事件总线
        LiveEventBus.with<Boolean>(Events.EVENT_LOGIN_SUCCESS).observe(this){
            if (it){
                onLogin()
                initData()

            }
        }

        mBinding.btnLogin.setOnClickListener{
            ARouter.getInstance().build(Router.Ui.LoginActivity).navigation()
        }


    }


}